
import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, ToastController } from 'ionic-angular';
import { BasePage } from "../../basePage";

import { newCity } from '../../../providers/cityData';
import { nailerService } from '../../../providers/nailerService';
import { StorageService } from '../../../providers/StorageService';
import { Utils } from '../../../providers/Utils';
declare var $: any

/**
 * Generated class for the TeacherNailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-teacher-nail',
  templateUrl: 'teacher-nail.html',
})
export class TeacherNailPage extends BasePage {
  msg2: string = '';
  msg: string = '';
  citydata: string[];

  user_info: any;
  selectList = '';

  cityColumns: any;
  fileObjList = [
    // {thumbPath:''}
  ];
  // getinfo = {
  //   name:'',
  //   phone:'',
  //   payPhone:'',
  // }
  regNailerObj = {
    artist_province: '',
    artist_city: '',
    artist_area: '',
    artist_name: '',
    artist_phone: '',
    alipay_account: '',
    artist_address: '',
    artist_avator:'',
    user_id:''
  }
  //判断是否注册过美甲师
  upLoad:boolean=true;

  serve1:any;//要上传的图片地址
  circle1Class='circle1';

  oboaut1 = 0;  //协议是否确认
  constructor(injector: Injector, public nailerservice: nailerService, public Storageservice: StorageService, public utils: Utils) {
    super(injector);
    this.cityColumns = newCity;
  }

  ionViewDidLoad() {
    this.user_info = this.Storageservice.read('userinfo');
    console.log(this.user_info);
    if(this.user_info.user_id != null){
      this.regNailerObj['user_id']=this.user_info.user_id;
    }
    //读取上次未提交的信息
    let obj=this.Storageservice.read("registNailer");
    let position=[];
    if(obj != null){
      for(let item in obj){
        console.log(item);
        this.regNailerObj[item]=obj[item];
      }
      position.push(obj['artist_province'],obj['artist_city'],obj['artist_area'])
      this.selectList=position.join(' ');
    }else{
      this.selectList="420000 420100 420111";
    }
    console.log(obj)
  }


  /**文本框修改
  * type:文本框类型
  * inputname：文本框的名称
  * placeholder：文本框的字段名
  * fontnum:文本框文字显示的个数
  * inputvalue:需要回填回去的值
  * 文本框打开方式
  * InputCLI('text','昵称','nickname',10)
  * this.openTextArea('nihao','姓名',data=>{
     
   });
  */
  InputCLI(type, inputname, placeholder, fontnum, keys) {
    let inputvalue = '';//后台获取的值

    this.openInputText(type, inputname, placeholder, fontnum, inputvalue, data => {
      console.log(data)
      /**判断手机 */
      if (keys == 'artist_phone') {
        this.msg = this.utils.checkphone(data);
        console.log(this.msg);
        /**手机填写正确 */
        if (this.msg == '') {
          for (let key in this.regNailerObj) {
            if (key == keys) {
              this.regNailerObj[key] = data;
            }
            console.log(this.regNailerObj);
          }
          /**手机填写错误 */
        } else {
          this.showAlert(this.msg, '提示');

        }
        /**判断支付宝 */
      } else if (keys == 'alipay_account') {
        this.msg = this.utils.checkalipay(data);
        /**支付宝填写正确 */
        if (this.msg == '') {
          for (let key in this.regNailerObj) {
            if (key == keys) {
              this.regNailerObj[key] = data;
            }
            console.log(this.regNailerObj);
          }
          /**支付宝填写错误 */
        } else {
          this.showAlert(this.msg, '提示');
        }
        /**判断姓名 */
      } else if (keys == 'artist_name') {
        for (let key in this.regNailerObj) {
          if (key == keys) {
            this.regNailerObj[key] = data;
          }
          console.log(this.regNailerObj);
          console.log(this.selectList)
        }
      }
    });
  }



  /**详细地址 */
  openTextAreaCLI() {
    this.openTextArea('详细地址', '', data => {
      if (typeof data != 'object') {
        this.regNailerObj.artist_address = data;
        console.log(data);
      }
    });
  }


  /**返回上一个页面 */
  dismissCLI() {
    //把地址和其他信息分别获取一起存起来，在提交的时候清除掉
    let writeP= this.selectList.split(' ');
    let obj=this.regNailerObj;
    obj.artist_province = writeP[0];
    obj.artist_city = writeP[1];
    obj.artist_area = writeP[2];
    this.Storageservice.write("registNailer",obj);
    this.viewCtrl.dismiss(1);
  }
  /** 选择1 */
  obout1(stu) {
    this.oboaut1 = stu;
    console.log(this.oboaut1);
  }

  lo() {
    console.log(this.selectList);
    this.citydata = this.selectList.split(' ');
    console.log(this.citydata);
  }

  /**开始认证美甲师 */
  async showConfirm() {
    this.citydata = this.selectList.split(' ');
    this.regNailerObj.artist_province = this.citydata[0];
    this.regNailerObj.artist_city = this.citydata[1];
    this.regNailerObj.artist_area = this.citydata[2];
    this.regNailerObj.artist_avator=this.serve1;
    if(this.regNailerObj.artist_name == ""||    //美甲师实际姓名
        this.regNailerObj.artist_phone==""||    //电话
        this.regNailerObj.alipay_account ==""||   //支付宝
        this.selectList=="" ||                     //现居地
        this.regNailerObj.artist_address=="" || //地址 
        this.serve1==undefined  //上传的图片
    ){                  
        this.showAlert("您的信息填写不完整，请确认填写完毕后提交！")
    }else if(this.oboaut1 == 0){
         this.showAlert("请先同意协议！")
    }else if(!/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.regNailerObj['artist_phone'])){
      let toast = this.toastCtrl.create({
        message:'手机号码格式不正确，请重新输入',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(!/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,6}){1,3}$/.test(this.regNailerObj['alipay_account']) && !/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.regNailerObj['alipay_account'])){
      let toast = this.toastCtrl.create({
        message:'支付宝格式不正确，请重新输入',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else{
      this.regnailerInfo();
    }
  }
  /**完成按钮 */
  async regnailerInfo() {
    let confirm = this.alertCtrl.create({
      // title: 'Use this lightsaber?',
      message: '您填写的信息将提交后台审核，认证通过后将通过消息通知您。',
      buttons: [
        {
          text: '继续填写',
          handler: () => {
            console.log('Disagree clicked');
          }
        },
        {
          text: '确认提交',
          handler: () => {
            this.nailerCorrect();
          }
        }
      ]
    });
    confirm.present();
  }

  async nailerCorrect (){
    this.Storageservice.removeItem("registNailer");
    let res = await this.nailerservice.registerNailArtist(this.regNailerObj);
    if (res.code == 1){
      let toast = this.toastCtrl.create({
        message: res.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
      setTimeout(() => {
        this.navCtrl.pop();
      }, 1500);
    } else {
      let toast = this.toastCtrl.create({
        message: res.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }

  
  //传图片
  returnapiimg(event){
    console.log("我是父页面");
    // console.log(event);
    // if (event.code != 1) {
    //   let toast = this.toastCtrl.create({
    //     message: event.msg,
    //     duration: 2000,
    //     position: 'top',
    //   })
    //   toast.present();
    // }else{
      let fileObjList=event[0];
      console.log('我是data里面的数据'+ fileObjList)
      // if(event.flagbs=='circle1'){
      console.log('进来了');
      this.upLoad=false;
      this.serve1=fileObjList;
      console.log(this.serve1+"ok");
      $(".img1").css("opacity","1");
      $(".lastc").css("height","19rem");
        // let modal = this.modalCtrl.create("TeacherNailPage",{serveImg:serve1});
        // modal.onDidDismiss(data => {
        
        // });
        // modal.present();
        // console.log('shop_loginname'+shop_logname);
        // this.userApi.shop_logo=shop_logname;
        // console.log('this.userApi.shop_logo'+this.userApi.shop_logo);
      

      // console.log(this.userApi);

    
  }

  
    // let reg= /^1[3|4|5|6|7|8|9]\d{9}$/;
    // let reg2= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   
}
